package org.cjlm.ui.pub;

import org.cjlm.ui.BoxMain;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

public abstract class MInternalFrame extends JInternalFrame {

    private BoxMain boxMain;

    private JDesktopPane desktop;

    private Map<String, MInternalFrame> frameRegistry;

    public MInternalFrame(String title, boolean max, int width, int height){
        super(title, max, true, max, max); // 启用所有控制按钮
        if(title == null || title.trim().length() == 0){
            this.setTitle(getDefaultTile());
        }
        setSize(width, height);

        // 窗口位置居中
        setLocation(
                (Toolkit.getDefaultToolkit().getScreenSize().width  - width) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height  - height) / 2
        );
    }

    public abstract String getDefaultTile();

    public void showErrorDialog(String title, String message, Exception e){
        JOptionPane.showMessageDialog(this, message+"："+e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
        throw new RuntimeException(e);
    }

    public void showErrorDialog(String title, String message){
        JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
    }

    public void showHintDialog(String title, String message){
        JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
    }

    public int showYesNoDialog(String title, String message){
        return JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_OPTION);
    }

    public void setDesktop(JDesktopPane desktop) {
        this.desktop = desktop;
    }

    public void setFrameRegistry(Map<String, MInternalFrame> frameRegistry) {
        this.frameRegistry = frameRegistry;
    }

    public JDesktopPane getDesktop() {
        return desktop;
    }

    public Map<String, MInternalFrame> getFrameRegistry() {
        return frameRegistry;
    }

    public BoxMain getBoxMain() {
        return boxMain;
    }

    public void setBoxMain(BoxMain boxMain) {
        this.boxMain = boxMain;
    }
}
